//<!--WTab配置参数-->
var WTabOption = function(){};
WTabOption.prototype = {
    container_id:'',
    page_id:'',
    self_id:''
};

var WTabListOption = function(){};
WTabListOption.prototype = {
    id:'tab_list_',
    title:'',
    url:'',
    isClosed:true
};

var bottom_heigth = 32;

function WTab(option){
    var tab_current = {
        current_tab:null,
        current_index:0,
        current_page:null
    };
    var w_op = new WTabOption();
    $.extend(w_op,option);
    this.id = w_op.self_id;
    this.page_id = w_op.page_id;
    this.tab_container = null;
    this.tabs = null;

    var tabTemp = '<table class="tab_list" id="{0}" border="0" cellpadding="0" cellspacing="0" style="top:0;left:{1}px"><tr>'
        +'<td class="tab_left"></td>'
        +'<td class="tab_center tab_title">{2}</td>'
        +'<td class="tab_center"><div class="tab_close"></div></td>'
        +'<td class="tab_right"></td>'
        +'</tr></table>';
    var tabContainerTemp = '<div class="tab_container" id="{0}"><div class="tab_hr"></div></div>';
    var pageContainer = '<iframe id="{0}" frameborder="0" src="" scrolling="no"></iframe>';
//    var pageContainer = '<iframe id="{0}" frameborder="0" src="{1}" scrolling-y="auto"></iframe>';
/*  width="100%" height="100%"  scrolling="no" style="overflow-y:auto;overflow-x:hidden;"*/
    $("#"+w_op.container_id).append(tabContainerTemp.replace("{0}",this.id));

    function init_tab(tab){
        var $the_tab = $(tab);
        if(tab_current.current_tab == null || tab_current.current_tab != this){
            $($the_tab).click(function(){
                if(tab_current.current_tab != null){
                    $(tab_current.current_tab).css("z-index",100)
                        .find(".tab_left").removeClass("tab_left_selected").addClass("tab_left_unselected").end()
                        .find(".tab_center").removeClass("tab_center_selected").addClass("tab_center_unselected").end()
                        .find(".tab_right").removeClass("tab_right_selected").addClass("tab_right_unselected").end();
//                        .find(".tab_close").removeClass("tab_close_selected");
//                                    .addClass("tab_close_unselected").end();
                }
                $the_tab.css("z-index",102)
                    .find(".tab_left").addClass("tab_left_selected").removeClass("tab_left_unselected").end()
                    .find(".tab_center").addClass("tab_center_selected").removeClass("tab_center_unselected").end()
                    .find(".tab_right").addClass("tab_right_selected").removeClass("tab_right_unselected").end()
                    .find(".tab_close").mouseover(function(){
                		$(this).removeClass("tab_close_unselected").addClass("tab_close_selected");
                	}).mouseout(function(){
                    	$(this).removeClass("tab_close_selected").addClass("tab_close_unselected");
                    }).click(function(){
                        close_tab($the_tab.attr("id"));
                    });
//                    .find(".tab_close").addClass("tab_close_selected");
//                                .removeClass("tab_close_unselected").end();
                tab_current.current_tab = this;
                show_page($(this).attr("id"),false);
            });
        }
    }
    function show_page(id,is_focus){
        if(is_focus){
            $("#"+id).trigger("click");
        }
        if(tab_current.current_page){
            tab_current.current_page.hide();
        }
        tab_current.current_page = $("#page_"+id);
        tab_current.current_page.show();
    }
    function close_tab(id){
        var tab_closed = $("#"+id);
        var page_closed = $("#page_"+id);
        if($(tab_current.current_tab).attr("id") == tab_closed.attr("id")){
            var next_tab = tab_closed.next();
            if(next_tab.attr("id")){
                show_page(next_tab.attr("id"),true);
            }else{
                var prev_tab = tab_closed.prev();
                if(prev_tab.attr("id")){
                    show_page(prev_tab.attr("id"),true);
                }
            }
        }
        var a_tab = tab_closed.next();
        var closed_width = tab_closed.width();
        while(a_tab.attr("id")){
            var l = 0;
            var left = a_tab.css("left");
            l = parseInt(left.substring(0,left.length-2));
            a_tab.css("left",l - closed_width + 3);
            a_tab = a_tab.next();
        }
        tab_closed.remove();
        page_closed.remove();
    }
    this.init = function(){
        this.tab_container = $("#" + this.id);
        this.tabs = this.tab_container.find(".tab_list");
        this.tabs.each(function(){
            init_tab(this);
        });
    };
    this.add = function(option){
    	
        var list_op = new WTabListOption();
        $.extend(list_op,option);
        
        list_op.title = "&nbsp;" + list_op.title + "&nbsp;";
        
        var page_html = pageContainer.replace("{0}", "page_"+list_op.id);
        $("#"+this.page_id).append(page_html);
        $("#page_"+list_op.id).attr("src",list_op.url);
        var tab_html = tabTemp.replace("{0}", list_op.id).replace("{2}", list_op.title);
        this.tab_container.append(tab_html);
        $("#" + list_op.id).css("z-index",100);
        $("#page_"+list_op.id).width($("#page").width());
        $("#page_"+list_op.id).height($("#page").height());

        var prev_tab = $("#" + list_op.id).prev();
        if(prev_tab.attr("id")){
            var left = prev_tab.css("left");
            if(left){
                var l = 0;
                l = parseInt(left.substring(0,left.length-2));
//                        alert(l + prev_tab.width());
                $("#" + list_op.id).css("left",l + prev_tab.width()-3);
            }else{
                $("#" + list_op.id).css("left",100);
            }
        }else{
            $("#" + list_op.id).css("left",100);
        }
//                if($("#" + list_op.id).prev()){
//                    var prev_tab=$("#" + list_op.id).prev();
//                    var left = prev_tab.css("left")?prev_tab.css("left")+prev_tab.width():10;
//                    $("#" + list_op.id).css("left",left);
//                }else{
//                    $("#" + list_op.id).css("left",10);
//                }
        init_tab($("#" + list_op.id));
        if(!list_op.isClosed){
            $("#"+list_op.id).find(".tab_close").hide();
        }else{
        	$("#"+list_op.id).find(".tab_close").addClass("tab_close_unselected");
        }
        this.show_page(list_op.id);
    };
    this.show_page = function(id){
        $("#" + id).trigger("click");
    };
    this.close_tab = function(id){
        close_tab(id);
    };
    this.init();
}
var add_tab = function(id,title,url){
	var totalTabs = $(".tab_list");
	if(totalTabs.length >= 8){
		alert("您打开的页面过多，请关闭部分页面后重试！");
		return false;
	}
    var tab_found = $(t.tab_container).find("#tab_list_"+id);
    if(tab_found.attr("id")){
        t.show_page('tab_list_'+id);
    }else{
        t.add({
            id:'tab_list_'+id,
            title:title,
            url:url,
            isClosed:true
        });
    }
};
var closeTab = function(id){
	t.close_tab(id);
};
$(document).ready(function(){
    $("#page").height($(window).height() - $("#page").position().top - bottom_heigth);

    $(window).resize(function(){
    	$("#page").height($(window).height() - $("#page").position().top - bottom_heigth);
    	$("#page").css("overflow-y","auto");
        
        $(document).find("iframe").width($(window).width());
        $(document).find("iframe").height($(window).height() - $("#page").position().top - bottom_heigth);
    });

    $(function(){
        var tab = new WTab({
            container_id:'tabs',
            page_id:'page',
            self_id:'f_tab'
        });
        tab.add({
            id:'tab_list_content1',
            title:'content1',
            url:'content1.html',
            isClosed:false
        });
		tab.add({
			id:'tab_list_content2',
			title:'content2',
			url:'content2.html',
			isClosed:true
		});
        t = tab;
		tab_index = 2;
    });
});